//
//  WYMediaTypeController.m
//  CirclePush
//
//  Created by admin on 2018/8/4.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMediaTypeController.h"
#import "WYMdeiaCell.h"
#import "WYAttribute.h"
#import "WYMediaReusableView.h"
#import "WYPriceRangeCell.h"
#import "WYUpModel.h"
@interface WYMediaTypeController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UIButton *sureButton;
@property (nonatomic,strong)UIButton *cancelButton;
@property (nonatomic,strong)NSMutableArray *collArray;
@property (nonatomic,strong)NSString *typeId;
@property (nonatomic,strong)WYUpModel *upmodel;
@end

@implementation WYMediaTypeController
-(NSMutableArray *)collArray
{
    if (!_collArray) {
        _collArray = [[NSMutableArray alloc]init];
    }
    return _collArray;
}
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self setupUI];
    [self loadData];
    self.view.userInteractionEnabled = YES;
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getMediaTypeSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [weakSelf.dataArray insertObject:[WYMediaType getMediaTypeFromJson:response[@"result"]] atIndex:0];
            [weakSelf.collectionView reloadData];
            }
    }];
}
-(void)getAttrByTypeId:(NSString *)typeID
{
    
    if (self.dataArray.count) {
        NSArray *mediaArray = self.dataArray[0];
        [self.dataArray removeAllObjects];
        [self.dataArray addObject:mediaArray];
    }
    WEAKSELF
    [WYHttpServer getMediaTypeWithTypeId:typeID SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            
            NSArray *array  =[WYAttrList getWYAttrListFromJson:response[@"result"]];
            if (array.count) {
                for (WYAttrList *list in array) {
                    [weakSelf.dataArray addObject:list];
                }
            }
            [weakSelf.collectionView reloadData];
        }
    }];
}








-(void)setupUI
{
    UIView *backView = [[UIView alloc]init];
    [self.view addSubview:backView];
    backView.frame = self.view.bounds;
    backView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.2];
    
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-50)/4, 40);
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    [layout setHeaderReferenceSize:CGSizeMake(kScreenWidth, 44)];
    
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, backView.height/3*2-50) collectionViewLayout:layout];
    self.collectionView = collection;
    [backView addSubview:collection];
    
    collection.delegate = self;
    collection.dataSource = self;
    collection.userInteractionEnabled = YES;
    [collection registerNib:[UINib nibWithNibName:@"WYMediaReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WYMediaReusableView"];
    collection.backgroundColor = [UIColor whiteColor];
    [collection registerNib:[UINib nibWithNibName:@"WYMdeiaCell" bundle:nil] forCellWithReuseIdentifier:@"WYMdeiaCell"];
    [collection registerNib:[UINib nibWithNibName:@"WYPriceRangeCell" bundle:nil] forCellWithReuseIdentifier:@"WYPriceRangeCell"];
    
    [backView addSubview:self.sureButton];
    [backView addSubview:self.cancelButton];
    
    
}
#pragma mark - uicollectionViewDatasoure

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
        if (section==0) {
            return [self.dataArray[section] count];
        }else{
            WYAttrList *list = self.dataArray[section];
            return list.list.count;
        }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        WYMdeiaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYMdeiaCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"WYMdeiaCell" owner:nil options:nil].firstObject;
        }
        [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
        [cell setCornerRadius:5.0];
        
        if (indexPath.section==0) {
            WYMediaType *type = self.dataArray[indexPath.section][indexPath.row];
            cell.textLabel.text = type.mediaTypeName;
            if (type.state) {
                [cell setBorderWithWidth:kLineHeight andColor:RGBA(250, 165, 122, 1.0)];
                [cell setBackgroundColor:RGBA(255, 247, 244, 1.0)];
                [cell setBackgroundColor:[UIColor whiteColor]];
                
            }else{
                [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
                [cell setCornerRadius:5.0];
                [cell setBackgroundColor:[UIColor whiteColor]];
            }
        }else{
            WYAttrList *list = self.dataArray[indexPath.section];
            WYAttribute *attr = list.list[indexPath.row];
            cell.textLabel.text = attr.mediaTypeName;
            if (attr.state) {
                [cell setBorderWithWidth:kLineHeight andColor:RGBA(250, 165, 122, 1.0)];
                [cell setBackgroundColor:RGBA(255, 247, 244, 1.0)];
                //attr.state = NO;
            }else{
                [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
                [cell setBackgroundColor:[UIColor whiteColor]];
                // attr.state = YES;
            }
        }
        return cell;
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader)
    {
        
        WYMediaReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WYMediaReusableView" forIndexPath:indexPath];
            if (indexPath.section==0) {
                headerView.textLabel.text=@"媒体类型";
            }else{
                WYAttrList *list = self.dataArray[indexPath.section];
                headerView.textLabel.text=list.attributeName;
            }
        reusableview = headerView;
    }
    reusableview.backgroundColor = RGBA(234, 234, 234, 234);
    return reusableview;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了某一个cell");
    if (indexPath.section==0) {
        for (WYMediaType *type in self.dataArray[0]) {
            type.state = NO;
        }
        WYMediaType *type = self.dataArray[indexPath.section][indexPath.row];
        type.state = YES;
        self.typeId = type.id;
        [self getAttrByTypeId:type.id];
        
        WYUpModel *upmodel = [[WYUpModel alloc]init];
        upmodel.id = type.id;
        upmodel.name = type.mediaTypeName;
       // [self.collArray addObject:upmodel];
        self.upmodel = upmodel;
        [self.collArray removeAllObjects];
    }else{
        WYMdeiaCell *cell = (WYMdeiaCell *)[collectionView cellForItemAtIndexPath:indexPath];
        WYAttrList *list = self.dataArray[indexPath.section];
        WYAttribute *attr = list.list[indexPath.row];
        if (attr.state) {
            [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
            [cell setBackgroundColor:[UIColor whiteColor]];
            attr.state = NO;
            [self.collArray removeObject:attr];
        }else{
            [cell setBorderWithWidth:kLineHeight andColor:RGBA(250, 165, 122, 1.0)];
            [cell setBackgroundColor:RGBA(255, 247, 244, 1.0)];
            attr.state = YES;
            [self.collArray addObject:attr];
        }
    }
    
    
}
-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]init];
        _sureButton.backgroundColor = RGBA(59, 128, 249, 1.0);
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        _sureButton.frame = CGRectMake(kScreenWidth/2, self.view.height/3*2-50, kScreenWidth/2, 50);
        [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:(UIControlEventTouchUpInside)];
        //_sureButton.hidden=YES;
    }
    
    
    return _sureButton;
}
-(UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]init];
        _cancelButton.backgroundColor =RGBA(235, 239, 245, 1.0);
        [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelButton setTitle:@"重置" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:RGBA(51, 51, 51, 1.0) forState:(UIControlStateNormal)];
        _cancelButton.frame = CGRectMake(0, self.view.height/3*2-50, kScreenWidth/2, 50);
//        _cancelButton.hidden=YES;
        [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cancelButton;
}
-(void)sureAction
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if (self.typeId.length) {
         [dic setValue:self.typeId forKey:@"typeID"];
    }
    if (self.collArray.count) {
        [dic setValue:self.collArray forKey:@"detail"];
    }
    if (self.upmodel) {
        [dic setValue:self.upmodel forKey:@"upModel"];
    }
   
    [[NSNotificationCenter defaultCenter] postNotificationName:@"mediaType" object:dic];
    [self.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeShow" object:nil];
}
-(void)cancelAction
{
//    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//    [dic setValue:@"" forKey:@"typeID"];
//    [dic setValue:@[] forKey:@"detail"];
//    [dic setValue:nil forKey:@"upModel"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"mediaTypeCancel" object:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeShow" object:nil];
    [self.view removeFromSuperview];
}
-(void)dealloc
{
    NSLog(@"界面竟然被销毁了？？？？");
}

@end
